package com.example.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.bean.Article;
import com.example.service.ArticleService;
import com.example.until.Result;
import com.example.until.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @title: ArticleController
 * @Author wjf
 * @Date: 2022/10/1 19:25
 * @Version 1.0
 */
@RestController
@CrossOrigin
@RequestMapping("/article")
@Scope("prototype")
public class ArticleController {

    @Autowired
    private ArticleService articleService;


    @GetMapping("/page")
    public Result<IPage<Article>> getAllUser(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "5") Integer pageSize,
            @RequestParam String name) {
        QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(name),"name",name);
        IPage<Article> page = new Page<>(current, pageSize);
        IPage<Article> page1 = articleService.page(page, queryWrapper);
        return Result.ok(page1);
    }

    @GetMapping({"/byName/{name}"})
    @CrossOrigin(allowCredentials = "true")
    public Result getUserByName(@PathVariable String name){
        QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(name),"name",name);
        Article one = articleService.getOne(queryWrapper);
        return Result.ok(one);
    }

    @GetMapping("/{id}")
    @CrossOrigin
    public Result<Article> getCourseById(@PathVariable("id") Integer id){
        Article article = articleService.getById(id);
        return Result.ok(article);
    }

    @GetMapping("/byTeacher/{id}")
    public Result getCourses(@PathVariable Integer id){
        QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("teacher_id",id);
        List<Article> list = articleService.list(queryWrapper);
        return Result.ok(list);
    }

    @PostMapping("/save")
    public Result<Boolean> saveUser(@RequestBody Article article) {
        article.setUser(TokenUtils.getCurrentUser().getNickname()); //获取当前用户的昵称并设置
        article.setTime(DateUtil.now());    //设置当前时间
        boolean save = articleService.save(article );
        return Result.ok(save);
    }

    @PutMapping("/update")
    public Result<Boolean> putUser(@RequestBody Article article ) {
        boolean b = articleService.updateById(article);
        return Result.ok(b);
    }

    @DeleteMapping("/delete/{id}")
    public Result<Boolean> deleteUser(@PathVariable Integer id) {
        boolean b = articleService.removeById(id);
        return Result.ok(b);
    }

    @DeleteMapping("/delete/batch/{ids}")
    public Result<Boolean> deleteMoreUser(@PathVariable List<Integer> ids){
        boolean b = articleService.removeByIds(ids);
        return Result.ok(b);
    }
}
